/**
 * class nwsim
 */

package nwsim;

public class NwSim {
	
	private static Node[] nodeArray;
	private static Channel channel;
	public static int ETX = 0;

	public static void main(String[] args) {
		channel = new Channel();
		initNodes();
		discoverNeighbors();
		System.out.println("Starting simulation...");
		startSimulation();
	}
	
	static void initNodes() {
		nodeArray = new Node[Constants.nodeCount];
		for(int i=0; i<Constants.nodeCount; i++) {
			nodeArray[i] = new Node(i, getCoordinates(i));
		}
	}
	
	static Coordinates getCoordinates(int nodeID) {
		Coordinates pos = new Coordinates();
		
		switch(nodeID) {
			
			case 0: pos.x = 0; 		pos.y = 0; 		break;
			case 1: pos.x = 500; 	pos.y = 0; 		break;
			case 2: pos.x = 100; 	pos.y = 200; 	break;
			default: System.out.println("Are you kidding me!!!"); break;
		}
		
		return pos;
	}
	
	static void discoverNeighbors() {
		for(int i=0; i<Constants.nodeCount; i++) {
			
			for(int j=0; j<Constants.nodeCount; j++) {
				if(i==j) continue;
				
				double prr = 1;
				if(i == 0) {					//source
					if(j==1)		prr = 1;		//dest		ACK to src
					else if(j==2)	prr = 1;		//relay		ACK relayed to src
				}
				else if(i == 1) {				//dest
					if(j==0)		prr = 0.3;		//source	Direct Native from src
					else if(j==2)	prr = 0.8;		//relay		Coop Native from relay
				}
				else if(i == 2) {				//relay
					if(j==0)		prr = 0.7;		//source	Direct Native to relay
					else if(j==1)	prr = 1;		//dest		ACK to relay
				}
				nodeArray[i].addNeighbor(j, prr);
				
					
			}
		}
	}
	
	public static Channel getChannel() {
		return channel;
	}
	
	static void startSimulation() {
		for(int i=0; i<Constants.nodeCount; i++) {
			nodeArray[i].start();
		}
	}
	
	public static void stopSimulation() {
		for(int i=0; i<Constants.nodeCount; i++) {
			nodeArray[i].stopThread = true;
			nodeArray[i].interrupt();
		}
		channel.cancel();
		System.exit(0);
	}
}
